package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.fowo.api.entity.StockIn;
import com.fowo.api.entity.StockOut;
import com.fowo.api.mapper.StockInMapper;
import com.fowo.api.mapper.StockOutMapper;
import com.fowo.api.model.requisition.RequisitionRevocationAction;
import com.fowo.api.model.requisition.RequisitionVo;
import com.fowo.api.service.RequisitionService;
import com.fowo.api.service.StockInService;
import com.fowo.api.service.StockOutService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RequisitionRevocation implements RequisitionRevocationAction {

    @Resource
    private RequisitionService requisitionService;
    @Resource
    private StockInMapper stockInMapper;
    @Resource
    private StockOutMapper stockOutMapper;

    @Override
    public void run(Long id) throws Exception {
        RequisitionVo requisitionVo = requisitionService.getVoById(id);

        new LambdaUpdateChainWrapper<>(stockInMapper)
                .set(StockIn::getStatus,"3")
                .eq(StockIn::getPurchaseOrderCode,requisitionVo.getRqtSn())
                .update();
        requisitionService.revocationGoodIn(id);
        requisitionService.revocationBadIn(id);

        new LambdaUpdateChainWrapper<>(stockOutMapper)
                .set(StockOut::getStatus,"CS04")
                .eq(StockOut::getCustomSourceSn,requisitionVo.getRqtSn())
                .update();
        requisitionService.revocationGoodOut(id);
        requisitionService.revocationBadOut(id);

        requisitionVo.setStatus("4");
        requisitionService.update(requisitionVo);
    }
}
